﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIListTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldSelectItemByNameFromComboBox()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.SelectItem("List Item 5");
            AutomationElement selection1 = listControl.GetSelectedItem();
            Assert.AreEqual("List Item 5", selection1.Current.Name);

            listControl.SelectItem("List Item 10");
            AutomationElement selection2 = listControl.GetSelectedItem();
            Assert.AreEqual("List Item 10", selection2.Current.Name);
        }

        [TestMethod]
        public void ShouldSetScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.SetScrollPercent(ScrollPattern.NoScroll, 100.0);
            UIListItem listItem = listControl.UIListItem("@Name='List Item 10'");
            Assert.IsFalse(listItem.IsOffscreen);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))] 
        public void ShouldThrowExceptionWhenSettingScrollPercentForInvalidDirection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.SetScrollPercent(50, 100.0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionWhenSettingScrollPercentGreaterThan100()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.SetScrollPercent(ScrollPattern.NoScroll, 101.0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionWhenSettingScrollPercentLessThan0()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.SetScrollPercent(ScrollPattern.NoScroll, -5.0);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldThrowExceptionWhenScrollingHorizontallyOnControlWithNoHorizontalScroll()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.ScrollHorizontally(ScrollAmount.SmallIncrement);
        }

        [TestMethod]
        public void ShouldScrollVertically()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.ScrollVertically(ScrollAmount.LargeIncrement);
            UIListItem listItem = listControl.UIListItem("@Name='List Item 10'");
            Assert.IsFalse(listItem.IsOffscreen);
        }

        [TestMethod]
        public void ShouldScrollToTop()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            listControl.SetScrollPercent(ScrollPattern.NoScroll, 100.0);
            UIListItem listItem10 = listControl.UIListItem("@Name='List Item 10'");
            Assert.IsFalse(listItem10.IsOffscreen);
            UIListItem listItem1 = listControl.UIListItem("@Name='List Item 1'");
            Assert.IsTrue(listItem1.IsOffscreen);
            listControl.ScrollToTop();
            Assert.IsFalse(listItem1.IsOffscreen);
        }

        [TestMethod]
        public void ShouldGetSelectedItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            UIListItem listItem1 = listControl.UIListItem("@Name='List Item 1'");
            listItem1.Select();
            Assert.IsTrue(listItem1.IsSelected);
            AutomationElement selectedItem = listControl.GetSelectedItem();
            Assert.IsNotNull(selectedItem);
            Assert.AreEqual("List Item 1", selectedItem.Current.Name);
        }

        [TestMethod]
        public void ShouldGetNoItemWhenThereIsNoSelectedItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            AutomationElement selectedItem = listControl.GetSelectedItem();
            Assert.IsNull(selectedItem);
        }

        [TestMethod]
        public void ShouldGetSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            var selection = listControl.GetSelection(); 
            Assert.AreEqual(0, selection.Length);

            UIListItem listItem1 = listControl.UIListItem("@Name='List Item 1'");
            listItem1.Select();
            selection = listControl.GetSelection(); 
            Assert.AreEqual(1, selection.Length);
        }

        [TestMethod]
        public void ShouldGetCanSelectMultiple()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.IsFalse(listControl.CanSelectMultiple);
        }

        [TestMethod]
        public void ShouldGetIsSelectionRequired()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.IsFalse(listControl.IsSelectionRequired);
        }

        [TestMethod]
        public void ShouldGetAllLists()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIList> lists = window.UILists();
            Assert.AreEqual(2, lists.Count);
        }

        [TestMethod]
        public void ShouldGetHorizontallyScrollable()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.IsFalse(listControl.HorizontallyScrollable);
        }

        [TestMethod]
        public void ShouldGetHorizontalScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.AreEqual(ScrollPattern.NoScroll, listControl.HorizontalScrollPercent);
        }

        [TestMethod]
        public void ShouldGetHorizontalViewSize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.AreEqual(100, listControl.HorizontalViewSize);
        }

        [TestMethod]
        public void ShouldGetVerticallyScrollable()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.IsTrue(listControl.VerticallyScrollable);
        }

        [TestMethod]
        public void ShouldGetVerticalScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.AreEqual(0, listControl.VerticalScrollPercent); 
        }

        [TestMethod]
        public void ShouldGetVerticalViewSize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.AreEqual(60, listControl.VerticalViewSize);
        }

        [TestMethod]
        public void ShouldGetColumnCount()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.AreEqual(-1, listControl.ColumnCount);
        }

        [TestMethod]
        public void ShouldGetRowCount()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIList listControl = window.UIList("@AutomationId='listBox1'");
            Assert.AreEqual(-1, listControl.RowCount);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}